package com.ljf.tijian.service;

import com.ljf.tijian.dto.ApiResponse;
import com.ljf.tijian.exception.TijianAppException;
import com.ljf.tijian.exception.TijianExceptionEnum;
import com.ljf.tijian.mapper.CheckItemMapper;
import com.ljf.tijian.mapper.CheckItemdetailedMapper;
import com.ljf.tijian.mapper.CidetailedreportMapper;
import com.ljf.tijian.mapper.CireportMapper;
import com.ljf.tijian.pojo.Cidetailedreport;
import com.ljf.tijian.pojo.Cireport;
import com.ljf.tijian.pojo.Orders;
import com.ljf.tijian.service.Impl.CiReportServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CiReportService implements CiReportServiceImpl {

    @Autowired
    CireportMapper cireportMapper;

    @Autowired
    CidetailedreportMapper cidetailedreportMapper;

    @Autowired
    CheckItemMapper checkItemMapper;

    @Autowired
    CheckItemdetailedMapper checkItemdetailedMapper;

    @Transactional
    @Override
    public ApiResponse createTemplate(Orders orders) {
        //1.排除一种情况,该订单已经创建模板
        int count = cireportMapper.selectOrderTemplate(orders.getOrderId());
        if(count>0){
            System.out.println("该报告模板已经出创建");
            throw  new TijianAppException(TijianExceptionEnum.REPORT_TEMPLATE_ALREADY_MAKE.getCode(), TijianExceptionEnum.REPORT_TEMPLATE_ALREADY_MAKE.getMessage());
        }
        //2.查询体检大项 往体检报告表中插入数据
        List<Cireport> cireports = checkItemMapper.selectCheckItemBySmId(orders.getSmId());
        for (Cireport cireport : cireports) {
            cireport.setOrderId(orders.getOrderId());
        }

        cireportMapper.insertBatch(cireports);

        //3.查询体检明细项，往体检报告明细表插入数据
        List<Cidetailedreport> cidetailedreports = checkItemdetailedMapper.selectCheckItemdetailedBySmId(orders.getSmId());
        for (Cidetailedreport cidetailedreport : cidetailedreports) {
            cidetailedreport.setOrderId(orders.getOrderId());
        }

        cidetailedreportMapper.insertBatch(cidetailedreports);

        return new ApiResponse(200,"SUCCESS");
    }
}
